package com.jt.tenant.action;

import android.content.Context;

import com.amap.api.services.weather.LocalDayWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/11.
 */
public class WeatherModular implements WeatherSearch.OnWeatherSearchListener{

    private WeatherSearch forcastsWeatherSearch, liveWeatherSearch;

    private List<OnWeatherCallBack> callBacks = new ArrayList<>();

    private LocalWeatherLive localWeatherLive;

    private List<LocalDayWeatherForecast> weathers;

    public WeatherModular(Context context){

        forcastsWeatherSearch = new WeatherSearch(context);

        forcastsWeatherSearch.setOnWeatherSearchListener(this);

        liveWeatherSearch = new WeatherSearch(context);

        liveWeatherSearch.setOnWeatherSearchListener(this);

    }

    public void setCity(String city){

        forcastsWeatherSearch.setQuery(new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_FORECAST));

        forcastsWeatherSearch.searchWeatherAsyn();

        liveWeatherSearch.setQuery(new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE));

        liveWeatherSearch.searchWeatherAsyn();

    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {

        if (localWeatherLiveResult != null && localWeatherLiveResult.getLiveResult() != null) {

            localWeatherLive = localWeatherLiveResult.getLiveResult();

            forecastLiveBackAll();

        }

    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

        if (localWeatherForecastResult!=null && localWeatherForecastResult.getForecastResult()!=null
                && localWeatherForecastResult.getForecastResult().getWeatherForecast()!=null
                && localWeatherForecastResult.getForecastResult().getWeatherForecast().size()>0) {

            weathers = localWeatherForecastResult.getForecastResult().getWeatherForecast();

            forecastCallBackAll();

        }

    }

    public void addOnWeatherCallBack(OnWeatherCallBack onWeatherCallBack){

        if(!callBacks.contains(onWeatherCallBack) && onWeatherCallBack != null){

            callBacks.add(onWeatherCallBack);

            liveCallBack(onWeatherCallBack);

            forecastCallBack(onWeatherCallBack);

        }

    }

    public void removeOnWeatherCallBack(OnWeatherCallBack onWeatherCallBack){

        if(!callBacks.contains(onWeatherCallBack) && onWeatherCallBack != null){

            callBacks.remove(onWeatherCallBack);

        }

    }

    private void liveCallBack(OnWeatherCallBack onWeatherCallBack){

        if(localWeatherLive != null){

            onWeatherCallBack.onWeatherLiveSearched(localWeatherLive);

        }

    }

    private void forecastCallBack(OnWeatherCallBack onWeatherCallBack){

        if(weathers != null){

            onWeatherCallBack.onWeatherForecastSearched(weathers);

        }

    }

    private void forecastLiveBackAll(){

        for (int i = 0; i < callBacks.size(); i++){

            liveCallBack(callBacks.get(i));

        }

    }

    private void forecastCallBackAll(){

        for (int i = 0; i < callBacks.size(); i++){

            forecastCallBack(callBacks.get(i));

        }

    }

    public static class OnWeatherCallBack{

        public void onWeatherLiveSearched(LocalWeatherLive localWeatherLive){}

        public void onWeatherForecastSearched(List<LocalDayWeatherForecast> weathers){}

    }

}
